package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.WxGatewayAddressDTO;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.entity.platform.WxGatewayAddress;
import com.ayf.payment.game.api.mapper.platform.WxGatewayAddressMapper;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.service.platform.WxGatewayAddressService;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WxGatewayAddressServiceImpl implements WxGatewayAddressService{

    @Resource
    private WxGatewayAddressMapper wxGatewayAddressMapper;

    @Autowired
    private WxConfigService wxConfigService;

    @Override
    public boolean inster(WxGatewayAddress wxGatewayAddress) {
        Example example = new Example(WxGatewayAddress.class);
        example.createCriteria().andEqualTo("userIp",wxGatewayAddress.getUserIp());
        WxGatewayAddress wxGatewayAddress1=wxGatewayAddressMapper.selectOneByExample(example);
        if(wxGatewayAddress1==null) {
            Example example_1 = new Example(WxGatewayAddress.class);
            example_1.createCriteria().andEqualTo("wxOriginId",wxGatewayAddress.getWxOriginId());
            int count=wxGatewayAddressMapper.selectCountByExample(example_1);
            WxConfig wxConfig=wxConfigService.getGameConfig(wxGatewayAddress.getWxOriginId());
            if(wxConfig.getId()>2 && count>=4) {
                return false;
            }
            return wxGatewayAddressMapper.insert(wxGatewayAddress) > 0;
        }else{
            wxGatewayAddress.setId(wxGatewayAddress1.getId());
            return wxGatewayAddressMapper.updateByPrimaryKey(wxGatewayAddress)>0;
        }
    }

    @Override
    public void update(WxGatewayAddress wxGatewayAddress) {
        wxGatewayAddressMapper.updateByPrimaryKeySelective(wxGatewayAddress);
    }

    @Override
    public boolean del(Integer id) {
        return wxGatewayAddressMapper.deleteByPrimaryKey(id) >0;
    }

    @Override
    public WxGatewayAddress getWxGatewayAddressById(Integer id) {

        return wxGatewayAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<WxGatewayAddressDTO> selectByWxOriginId(String wxOriginId,Integer merchantId) {
        if(StringUtils.isEmpty(wxOriginId)){
            Example example = new Example(WxGatewayAddress.class);
            example.createCriteria().andEqualTo("merchantId",merchantId);
            List<WxGatewayAddress>list=wxGatewayAddressMapper.selectByExample(example);
            return BeanConvertUtils.convert(list,WxGatewayAddressDTO.class);
        }
        Example example = new Example(WxGatewayAddress.class);
        example.createCriteria().andEqualTo("wxOriginId",wxOriginId);
        List<WxGatewayAddress>list=wxGatewayAddressMapper.selectByExample(example);
        return BeanConvertUtils.convert(list,WxGatewayAddressDTO.class);
    }
}
